package homework;

import java.util.Scanner;

public class StudentUI {
    Scanner scanner = new Scanner(System.in);
    SuperA superA = new SuperA();

    public void menu(){
        while (true){
            System.out.println("1、添加学生信息  2、删除学生信息  3、查询学生信息  4、修改学生信息  5、退出");
            int index = scanner.nextInt();
            switch (index){
                case 1:
                    add();
                    break;
                case 2:
                    delete();
                    break;
                case 3:
                    lookup();
                    break;
                case 4:
                    revise();
                    break;
                case 5:
                    System.exit(1);
                default:
                    System.out.println("输入有误，请重新输入");
                    continue;
            }
        }
    }


    private void add() {
        System.out.println("请输入姓名：");
        String name = scanner.next();
        System.out.println("请输入年龄：");
        int age = scanner.nextInt();
        System.out.println("请输入性别：");
        String sex = scanner.next();
        superA.add(new Student(name,age,sex));
    }

    private void delete() {
        System.out.println("请输入学生姓名：");
        String name = scanner.next();
        for (int i = 0;i < superA.size();i++){
            Student student = (Student) superA.get(i);
            if (student.getName().equals(name)){
                superA.remove(i);
                System.out.println("删除成功");
                return;
            }
        }
    }

    private void lookup() {
        System.out.println("请输入学生姓名：");
        String name = scanner.next();
            for (int i = 0; i < superA.size(); i++){
                Student student =  (Student) superA.get(i);
                if (student.getName().equals(name)){
                    System.out.println("姓名：" + student.getName() + "年龄：" + student.getAge() + "性别：" + student.getSex());
                    return;
                }
            }
        }

    private void revise() {
        System.out.println("请输入需要修改的学生姓名：");
        String name = scanner.next();
        for (int i = 0; i < superA.size(); i++){
            Student student =  (Student) superA.get(i);
            if (student.getName().equals(name)){
                System.out.println("请输入新学生的姓名: ");
                String newName = scanner.next();
                System.out.println("请输入新学生的年龄: ");
                int newAge = scanner.nextInt();
                System.out.println("请输入新学生的性别: ");
                String newSex = scanner.next();
                student.setName(newName);
                student.setAge(newAge);
                student.setSex(newSex);
                System.out.println("修改成功");
                return;
            }
        }
    }

    public static void main(String[] args) {
        StudentUI studentUI = new StudentUI();
        studentUI.menu();
    }
}
